package boun.swe.bob.service;

import boun.swe.bob.entities.Friend;
import boun.swe.bob.entities.FriendRequest;
import boun.swe.bob.entities.User;
import com.google.common.collect.Maps;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * This controller is used for my friends page actions
 */
public class MyFriendsController extends BaseController {

    /**
     * Shows the page
     *
     * @param httpServletRequest http request
     * @param httpServletResponse http response
     * @return page
     */
    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView showPage(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {

        String requestedUserIdStr = httpServletRequest.getParameter("requestedUserId");
        //String wantedUserIdStr = httpServletRequest.getAttribute("wantedUserId");
        String wantedUserIdStr = httpServletRequest.getParameter("wantedUserId");
        String actionType=httpServletRequest.getParameter("action");
        if(null != requestedUserIdStr) {
            int requestedUserId = Integer.parseInt(requestedUserIdStr);

            if(null !=httpServletRequest.getParameter("action"))
            {

            if(actionType.equals("add"))
            {
            FriendRequest friendRequest = new FriendRequest();
            friendRequest.setRequestedUserId(requestedUserId);
            friendRequest.setRequestingUserId(getLoggedInUser(httpServletRequest).getUserId());
            friendRequestDao.save(friendRequest);

            Friend friend = new Friend();
            friend.setUser1Id(getLoggedInUser(httpServletRequest).getUserId());
            friend.setUser2Id(requestedUserId);
            friend.setApproved(0);
            friendDao.save(friend);

            friend = new Friend();
            friend.setUser1Id(requestedUserId);
            friend.setUser2Id(getLoggedInUser(httpServletRequest).getUserId());
            friend.setApproved(0);
            friendDao.save(friend);
                }
                else
                if(actionType.equals("remove"))
                {
                    friendDao.deleteFriend(requestedUserId,getLoggedInUser(httpServletRequest).getUserId());
                }
                }
        }

        if(wantedUserIdStr != null /*&& !actionType.equals("add") && !actionType.equals("remove")*/){
            Map<String, Object> myModel = Maps.newHashMap();
            User requestedUser = userDao.findByPrimaryKey(Integer.parseInt(wantedUserIdStr));
            myModel.put("user", requestedUser);
            return new ModelAndView("myFriends", "model", myModel);
        }
        else{

            Map<String, Object> myModel = Maps.newHashMap();
            myModel.put("himself", true);
            User loggedInUser = userDao.findByPrimaryKey(getLoggedInUser(httpServletRequest).getUserId());
            myModel.put("user", loggedInUser);
            httpServletRequest.getSession().setAttribute("user", loggedInUser);
            return new ModelAndView("myFriends", "model", myModel);
        }

    
    }

}
